﻿#include "oglmanager.h"
#include <QKeyEvent>
#include <QDebug>
#include "resourcemanager.h"
#include "cube.h"

const QVector3D CAMERA_POSITION(0.0f, 0.0f, 3.0f);
const QVector3D LIGHT_POSITION(1.0f, 0.8f, 0.8f);

Cube *cube;

OGLManager::OGLManager(GLuint w, GLuint h)
{
    this->width = w;
    this->height = h;
    for (GLuint i = 0; i != 1024; ++i)
        keys[i] = GL_FALSE;

}

OGLManager::~OGLManager()
{
    delete this->camera;
    ResourceManager::clear();
}

void OGLManager::init()
{
    cube = new Cube();

    this->camera = new Camera(CAMERA_POSITION);
    cube->init();
    core = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_3_3_Core>();

    ResourceManager::loadShader("cube", ":/shaders/res/shaders/cube.vert", ":/shaders/res/shaders/cube.frag");
    ResourceManager::loadShader("light", ":/shaders/res/shaders/light.vert", ":/shaders/res/shaders/light.frag");

    ResourceManager::getShader("cube").use().setInteger("material.diffuse", 0);
    ResourceManager::getShader("cube").use().setInteger("material.specular", 1);
    ResourceManager::getShader("cube").use().setFloat("material.shininess", 64.0f);

    ResourceManager::getShader("cube").use().setVector3f("light.ambient", QVector3D(0.2f, 0.2f, 0.2f));
    ResourceManager::getShader("cube").use().setVector3f("light.diffuse", QVector3D(0.5f, 0.5f, 0.5f));
    ResourceManager::getShader("cube").use().setVector3f("light.specular", QVector3D(1.0f, 1.0f, 1.0f));

    // spot light定义 - 手电筒(Flashlight)是一个位于观察者位置的聚光，通常它都会瞄准玩家视角的正前方。基本上说，手电筒就是普通的聚光，但它的位置和方向会随着玩家的位置和朝向不断更新。
    ResourceManager::getShader("cube").use().setFloat("light.cutOff", cos(12.5f / 180.f * 3.14)); // 切光角为12.5度，传入余弦值给片段着色器
    ResourceManager::getShader("cube").use().setVector3f("light.position", camera->position);
    ResourceManager::getShader("cube").use().setVector3f("light.direction", camera->front);

    // 路灯效果
//    ResourceManager::getShader("cube").use().setVector3f("light.position", LIGHT_POSITION);
//    ResourceManager::getShader("cube").use().setVector3f("light.direction", QVector3D(0.0f, -1.0f, 0.0f));

    ResourceManager::getShader("cube").use().setFloat("light.outerCutOff", cos(17.5f / 180.f * 3.14));

    ResourceManager::getShader("light").use().setVector3f("lightColor", QVector3D(1.0f, 1.0f, 1.0f));


    QMatrix4x4 model;
    ResourceManager::getShader("cube").use().setMatrix4f("model", model);
    model.translate(LIGHT_POSITION);
    model.scale(0.2f);
    ResourceManager::getShader("light").use().setMatrix4f("model", model);


    //开启状态
    core->glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
    core->glEnable(GL_DEPTH_TEST);
}

void OGLManager::processInput(GLfloat dt)
{
    if (keys[Qt::Key_W])
        camera->processKeyboard(FORWARD, dt);
    if (keys[Qt::Key_S])
        camera->processKeyboard(BACKWARD, dt);
    if (keys[Qt::Key_A])
        camera->processKeyboard(LEFT, dt);
    if (keys[Qt::Key_D])
        camera->processKeyboard(RIGHT, dt);
    if (keys[Qt::Key_E])
        camera->processKeyboard(UP, dt);
    if (keys[Qt::Key_Q])
        camera->processKeyboard(DOWN, dt);
}


void OGLManager::update(GLfloat dt)
{
    QMatrix4x4 projection, model;
    projection.perspective(camera->zoom, (GLfloat)width / (GLfloat)height, 0.1f, 200.f);

    ResourceManager::getShader("cube").use().setMatrix4f("projection", projection);
    ResourceManager::getShader("cube").use().setMatrix4f("view", camera->getViewMatrix());
    ResourceManager::getShader("cube").use().setVector3f("viewPos", camera->position);

    ResourceManager::getShader("light").use().setMatrix4f("projection", projection);
    ResourceManager::getShader("light").use().setMatrix4f("view", camera->getViewMatrix());
}

void OGLManager::resize(GLuint w, GLuint h)
{
    core->glViewport(0, 0, w, h);
}

QVector3D cubePositions[] =
{
    QVector3D( 0.0f,  0.0f,  0.0f),
    QVector3D( 2.0f,  5.0f, -15.0f),
    QVector3D(-1.5f, -2.2f, -2.5f),
    QVector3D(-3.8f, -2.0f, -12.3f),
    QVector3D( 2.4f, -0.4f, -3.5f),
    QVector3D(-1.7f,  3.0f, -7.5f),
    QVector3D( 1.3f, -2.0f, -2.5f),
    QVector3D( 1.5f,  2.0f, -2.5f),
    QVector3D( 1.5f,  0.2f, -1.5f),
    QVector3D(-1.3f,  1.0f, -1.5f)
};

void OGLManager::draw(GLfloat dt)
{
    core->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

//  ResourceManager::getShader("light").use();
//  cube->drawLight();

    for (unsigned int i = 0; i < 10; i++)
    {
        QMatrix4x4 model;
        model.translate(cubePositions[i]);
        GLfloat angle = 20.0f * i;
        model.rotate(angle, QVector3D(1.0f, 0.3f, 0.5f));
        ResourceManager::getShader("cube").use().setMatrix4f("model", model);

        cube->drawCube();
    }
}
